﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace LibSnesDotNet
{
	static class Extensions
	{
		static void ValidateHelper(object configObject)
		{
			foreach (object o in (from PropertyInfo pi in configObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
			                      where pi.GetCustomAttributes(typeof(ConfigurationPropertyAttribute), false).Length > 0
			                      select pi.GetValue(configObject, null)))
			{
				if (o is ConfigurationElement)
				{
					((ConfigurationElement)o).Validate();
				}
				else if (o is ConfigurationElementCollection)
				{
					((ConfigurationElementCollection)o).Validate();
				}
			}
		}

		public static void Validate(this ConfigurationSection section)
		{
			ValidateHelper(section);
		}

		public static void Validate(this ConfigurationElement element)
		{
			ValidateHelper(element);
		}

		public static void Validate(this ConfigurationElementCollection collection)
		{
			foreach (ConfigurationElement element in collection)
			{
				element.Validate();
			}
		}

		public static void Validate(this ConfigurationSectionGroup group)
		{
			foreach (ConfigurationSection section in group.Sections)
			{
				section.Validate();
			}
		}
	}
}
